//
//  LeftViewController.m
//  yameishe1
//
//  Created by 吴坤金 on 2017/2/28.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "LeftViewController.h"
#import "RootHeadView.h"
#import "MediaSourceController.h"

@interface LeftViewController ()<UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource>
{
    NSArray *functionArray;
    NSMutableArray *headViews;

    id options;
    int selectIndex;

}
@property (nonatomic,weak) IBOutlet UITableView *tableView;
@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    functionArray = [[NSArray alloc]initWithObjects:@"网络媒体",@"户外媒体",@"平面媒体",@"电视媒体",@"广播媒体",@"记者爆料",@"内容代写",@"宣传定制", nil];
    [self initdata];
    [self initTableView];
    [_tableView reloadData];
    self.view.backgroundColor = MainColor;
    self.navigationController.delegate = self;
}
-(void)initdata
{
    id arr1 = @[
                @[@"新闻发布", @"MediaSourceController",@"Network",    @"NewsReleaseController"],
                @[@"百科营销", @"MemberTypeController",@"MemberTypeController",    @"70104"],
                @[@"短视频", @"MemberTypeController",@"MemberTypeController",    @"70104"],
                @[@"公众号", @"FBPublicNumberController",@"Network",@""],
                @[@"论坛", @"MemberTypeController",@"MemberTypeController",    @"70104"],
                @[@"秒拍", @"SecondShotController",@"Network", @""],
                @[@"文案策划", @"MemberTypeController",@"MemberTypeController",    @"70104"],
                @[@"微博大号", @"MemberTypeController",@"MemberTypeController",    @"70104"],
                @[@"微信营销", @"MemberTypeController",@"MemberTypeController",    @"70104"],
                @[@"问答营销", @"MemberTypeController",@"MemberTypeController",    @"70104"],
                ];
    id arr2 = @[
                @[@"地标媒体", @"ProductTypeController",    @"70102"],
                @[@"户外大牌", @"ProductUnitController",    @"70101"],
                @[@"户外频煤", @"ProductArchivesController",    @"70105"],
                @[@"公交媒体", @"ProductUnitController",    @"70101"],
                @[@"地铁媒体", @"ProductArchivesController",    @"70105"],
                @[@"楼宇媒体", @"ProductUnitController",    @"70101"],
                @[@"机场媒体", @"ProductArchivesController",    @"70105"],
                @[@"高铁媒体", @"ProductUnitController",    @"70101"],
                @[@"影院媒体", @"ProductArchivesController",    @"70105"],
                @[@"高速媒体", @"PresentArchivesController",    @"70103"],
                ];
//    id arr3 = @[
//                @[@"门店信息", @"ShopInformationController",    @"70107"],
//                @[@"商家信息", @"BusinessInfoController",    @"70401"],
//                
//                ];
//    id arr4 = @[
//                @[@"员工档案", @"EmployeeArchivesController",    @"70108"],
//                
//                ];
    options = @[
                //0标题(title)  1图片(img)               2是否伸展(isopen)     3界面(ctrl)        4权限编号(funcid)  5arr
                @[@"网络媒体",   @"ic_basic_memberArchives",   @YES,   @"StockManager",   @"",          arr1],
                @[@"户外媒体",   @"ic_basic_productArchives",    @YES,   @"StockManager",   @"",          arr2],
                @[@"平面媒体",   @"ic_basic_shopArchives",      @NO,    @"ShopInformationController",   @""  ],
                @[@"电视媒体",   @"ic_basic_shopArchives",      @NO,    @"ShopInformationController",   @""  ],
                @[@"广播媒体",   @"ic_basic_shopArchives",      @NO,    @"ShopInformationController",   @""  ],
                @[@"记者爆料",   @"ic_basic_shopArchives",      @NO,    @"ShopInformationController",   @""  ],
                @[@"内容代写",   @"ic_basic_shopArchives",      @NO,    @"ShopInformationController",   @""  ],
                @[@"宣传定制",   @"ic_basic_empolyeeArchives",    @NO,   @"EmployeeArchivesController",   @"",          ]
                ];
}
-(void)initTableView
{
    headViews = [[NSMutableArray alloc]initWithCapacity:[options count]];
    selectIndex = -1;
    int index = 0;
    for (id obj in options) {
        RootHeadView *view = [[[NSBundle mainBundle]loadNibNamed:@"RootHeadView" owner:nil options:nil]firstObject];
        view.labelTitle.text = obj[0];
        [view.imgIcon setImage:[UIImage imageNamed:obj[1]]];
        BOOL isopen = [obj[2] boolValue];
        view.isExpand = isopen;
        view.index = index++;
        [view addTarget:self action:@selector(onClickHeader:) forControlEvents:UIControlEventTouchUpInside];
        view.viewLine.height = 0.5f;
        [headViews addObject:view];
        
    }
    
    
//    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-2*64)];
//    [tableView tableViewSetting];
//    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
//    
//    
//    tableView.delegate = self;
//    tableView.dataSource = self;
//    
//    _headView=[[[NSBundle mainBundle]loadNibNamed:@"ASKHomeHeaderView" owner:self options:nil] firstObject];
//    tableView.tableHeaderView=_headView;
//    
//    [self.view addSubview:tableView];
//    _tableView =tableView;
    _tableView.tableFooterView = [[UIView alloc]init];
    [_tableView registerNib:[UINib nibWithNibName:@"RootSubCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
}
#pragma mark -tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [options count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    RootHeadView *view = headViews[section];
    if (view.isEnabled) {
        if (!view.isOpend) {
            return 0;
        }
        return [options[section][5]count];
    }
    else
    {
        return 0;
    }
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor clearColor];
    UILabel *label = (UILabel*)[cell.contentView viewWithTag:1];
    id obj = options[indexPath.section][5][indexPath.row];
    label.text = obj[0];
    return cell;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return headViews[section];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RootHeadView *view = headViews[indexPath.section];
    return view.isOpend?42:0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 48;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    id obj = options[indexPath.section][5][indexPath.row];
    
    
    NSString *ctrname = obj[1];
    NSString *func    = obj[3];
    NSString *story = obj[2];
    [self gotoController:ctrname withFunc:func withStory:story];
}

-(void)onClickHeader:(id)sender
{
    RootHeadView *head = sender;
    int index = head.index;
    
    if (head.isExpand) {
        if (index != selectIndex && selectIndex != -1) {
            RootHeadView *view = headViews[selectIndex];
            view.isOpend = NO;
        }
        head.isOpend = !head.isOpend;
        selectIndex = head.isOpend?index:-1;
        [_tableView reloadData];
    }
    else
    {
        id obj = options[index];
        NSString *ctrName = obj[3];
        NSString *func = obj[4];
        [self gotoController:ctrName withFunc:func withStory:ctrName];
    }
    
}

-(void)gotoController:(NSString*)ctrName withFunc:(NSString*)funcCode withStory:(NSString*)story
{
    if (ctrName == nil || story == nil) {
        return;
    }
    
//    if (funcCode && ![funcCode isEmpty]) {
//        id params = @{
//                      @"InterfaceCode"  : @"20660622",
//                      @"ModuleCode"     : funcCode,
//                      @"userid"         : [ASKMember loginMember].UserID
//                      };
//        
//        [[ASKAPI sharedInstance]requestWithURL:nil params:params successBlock:^(id responseObject) {
//            
//            UIViewController *ctrl = [[UIStoryboard storyboardWithName:story bundle:nil]instantiateViewControllerWithIdentifier:ctrName];
//            ctrl.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:ctrl animated:YES];
//            
//        } failureBlock:^(NSString *error) {
//            [ViewUtil alertWithMessage:error cancelButtonTitle:@"确定"];
//        }];
        
        
//    }
//    else
//    {
        if (_callbackDidSelect) {
            _callbackDidSelect();
        }
        UIViewController *ctrl = [[UIStoryboard storyboardWithName:story bundle:nil]instantiateViewControllerWithIdentifier:ctrName];
        if ([ctrl isKindOfClass:[MediaSourceController class]]) {
            MediaSourceController *sourceController = (MediaSourceController*)ctrl;
            sourceController.type = funcCode;
        }
        UINavigationController *nav = (UINavigationController *)self.mm_drawerController.centerViewController;
        nav.view.backgroundColor = [UIColor whiteColor];
        [nav pushViewController:ctrl animated:YES];
//    }
}

#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowViewController = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowViewController animated:YES];
    [_tableView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated {
    // 反注册键盘
    [super viewWillDisappear:YES];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


